package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;
    /*
     * 分页条件查询
     * */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        Page<Address> addressPage = addressDao.findByCondition(queryString);
        return new PageResult(addressPage.getTotal(),addressPage.getResult());
    }


    /*
     * 新增
     * */
    @Override
    public void add(Address address) {
        addressDao.add(address);
    }

    /*
     * 根据id查询address对象回显
     * */
    @Override
    public Address findByid(Integer id) {
        return addressDao.findByid(id);
    }
    /*
     * 修改体检机构
     * */
    @Override
    public void update(Address address) {
        addressDao.update(address);
    }

    /*
     * 根据id删除
     * */
    @Override
    public void delete(Integer id) throws RuntimeException {
        int count = addressDao.findByAddressId(id);
        if(count>0){
            throw  new RuntimeException();
        }else {
            addressDao.delete(id);
        }

    }
}
